package com.zyg.seckill.controller;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import com.zyg.common.entity.PayVo;
import com.zyg.common.utils.MyDate;
import com.zyg.seckill.client.OrderClient;
import com.zyg.seckill.service.TbSeckillOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * ------------------------------
 * 功能：
 * 作者：WF
 * 微信：hbxfwf13590332912
 * 创建时间：2021/11/26-16:30
 * ------------------------------
 */
@Controller
public class SeckillOrderController {
    @Autowired
    private TbSeckillOrderService orderService;
    @Autowired
    private OrderClient orderClient;
    //下单
    @GetMapping("saveOrder")
    public String saveOrder(String secKillId, Model model, HttpServletRequest request){
        //1. 得到登录用户
        XxlSsoUser ssoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        //2. 得到时间段字符串
        String time = MyDate.getDateStr(MyDate.getDateMenus().get(0));
        //3. 保存订单
        boolean b =  orderService.saveOrder(secKillId,ssoUser.getUsername(),time);
        //4. 为支付页面指定订单编号
        model.addAttribute("outTrandNo",secKillId);
        return "pay";
    }

    //秒杀支付--->同步调用
    @GetMapping("pay")
    @ResponseBody
    public  String pay(){
       PayVo payVo =  orderService.findPayVo("zelin");
       return orderClient.pay(2, payVo.getOut_trade_no());
    }

    @GetMapping("orderlist")
    String orderList(Model model){
        return orderClient.orderList(model);
    }
}
